﻿Namespace langsamu.PlaceholdIt.Web.UI.WebControls
    Public Class Image
        Inherits System.Web.UI.WebControls.Image

        Public Overrides Property ImageUrl As String
            Get
                If String.IsNullOrEmpty(MyBase.ImageUrl) Then
                    Return PlaceholdItImageUrl
                Else
                    Return MyBase.ImageUrl
                End If
            End Get
            Set(ByVal value As String)
                MyBase.ImageUrl = value
            End Set
        End Property
        Public Overrides Property Width As System.Web.UI.WebControls.Unit
            Get
                If String.IsNullOrEmpty(MyBase.ImageUrl) AndAlso MyBase.Width.IsEmpty Then
                    Return System.Web.UI.WebControls.Unit.Pixel(300)
                Else
                    Return MyBase.Width
                End If
            End Get
            Set(ByVal value As System.Web.UI.WebControls.Unit)
                MyBase.Width = value
            End Set
        End Property
        Public Property ImageFormat As langsamu.PlaceholdIt.ImageFormat
            Get
                Return ViewState!ImageFormat
            End Get
            Set(ByVal value As langsamu.PlaceholdIt.ImageFormat)
                ViewState!ImageFormat = value
            End Set
        End Property

        Private ReadOnly Property PlaceholdItImageUrl() As String
            Get
                Dim urlBuilder = New System.Text.StringBuilder

                urlBuilder.Append("http://placehold.it/")

                urlBuilder.Append(Me.Width.Value)

                If Not MyBase.Height.IsEmpty Then
                    urlBuilder.AppendFormat("x{0}", MyBase.Height.Value)
                End If

                If ImageFormat <> PlaceholdIt.ImageFormat.gif Then
                    urlBuilder.AppendFormat(".{0}", ImageFormat)
                End If

                If Not MyBase.BackColor.IsEmpty Then
                    urlBuilder.AppendFormat("/{0}", ToHtmlHexColor(MyBase.BackColor))
                ElseIf Not MyBase.ForeColor.IsEmpty Then
                    urlBuilder.Append("/cccccc")
                End If

                If Not MyBase.ForeColor.IsEmpty Then
                    urlBuilder.AppendFormat("/{0}", ToHtmlHexColor(MyBase.ForeColor))
                End If

                If Not String.IsNullOrEmpty(MyBase.AlternateText) Then
                    urlBuilder.AppendFormat("&text={0}", MyBase.AlternateText)
                ElseIf Not String.IsNullOrEmpty(MyBase.ToolTip) Then
                    urlBuilder.AppendFormat("&text={0}", MyBase.ToolTip)
                End If

                Return urlBuilder.ToString
            End Get
        End Property
        Private Function ToHtmlHexColor(ByVal original As System.Drawing.Color) As String
            Return String.Format("{0:X2}{1:X2}{2:X2}", original.R, original.G, original.B)
        End Function
    End Class
End Namespace
